package com.cuit.job;

import com.cuit.factory.SingleFactory;
import com.cuit.limit.FlowController;
import com.cuit.limit.ServiceFlowManager;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class FlowMetricsJob implements Job{

    @Override
    public void doJob(){
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        ServiceFlowManager serviceFlowManager = SingleFactory.getSingle(ServiceFlowManager.class);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            Set<Map.Entry<String, FlowController>> entries = serviceFlowManager.getServiceFlowController().entrySet();
            for (Map.Entry<String, FlowController> entry : entries){
                FlowController value = entry.getValue();
                String serviceName = entry.getKey();
                log.info("==========客户端统计调用次数===========");
                log.info("[{}]上一秒调用次数[{}]", serviceName, value.getLastCallCountAtLastMinute());
                log.info("[{}]当前秒调用次数[{}]", serviceName, value.getCurrentCallCount());
                log.info("[{}]下一秒调用次数[{}]", serviceName, value.getNextCallCount());
                log.info("====================================");
            }
        }, 5, 60, TimeUnit.SECONDS);
    }

}
